import { NextRequest, NextResponse } from 'next/server';
import { authConfig, authMiddleware } from './middleware/authMiddleware';

const middlewares = [
  {
    handler: authMiddleware,
    matcher: authConfig.matcher.map((item) => {
      return new RegExp(item.replace(':path*', '.*'));
    }),
  },
];

/**
 * @author        lx <1154354121@qq.com>
 * @date          2025-04-14 11:10:13
 * Copyright © YourCompanyName All rights reserved
 */
export async function middleware(request: NextRequest) {
  for (const middleware of middlewares) {
    if (
      middleware.matcher.some((item) => item.test(request.nextUrl.pathname))
    ) {
      const res = await middleware.handler(request);

      if (res !== NextResponse.next()) return res;
    }
  }

  return NextResponse.next();
}

export const config = {
  matcher: [
    '/((?!_next/static|_next/image|_next/data|images|fonts|locales|static|public|favicon.ico|robots.txt|sitemap.xml|.*\\.(?:json|xml|txt|ico|svg|png|jpg|jpeg|gif|webp|css|js|woff|woff2|ttf|eot)$).*)',
  ],
};
